import { queryType } from '@/utils'
export default {
  data () {
    return {
      requery: {
        page: 1,
        size: 20,
        total: 0,
      },
      data_loading: false,
      autoQuery: true
    }
  },
  computed: {
    hasMore () {
      // 是否有更多数据
      if ((this.requery.page * this.requery.size) < this.requery.total) {
        return true
      }
      return false
    },
    noMore () {
      // 数据已加载完
      if (Math.ceil(this.requery.total / this.requery.size) == this.requery.page) {
        return true
      }
      return false
    },
  },
  methods: {
    loadMore (cb) {
      // 查询更多数据
      if (!this.hasMore) {
        return
      }
      this.loadData(this.requery.page + 1, cb).catch(_ => { })
    },
    loadData (currentPage, cb) {
      let current = queryType(currentPage) == 'Number' 
      // 查询列表 使用前定义 queryData header 会自动填充
      return new Promise((resolve, reject) => {
        this.data_loading = true
        uni.showLoading({
          title: '数据加载中...',
          mask: true
        })
        let queryData = {
          ...this.queryData.data
        }
        if (this.queryData.page) {
          queryData.size = this.requery.size
          queryData.page = current ? currentPage : this.requery.page
        }
        console.log('query', this.queryData, queryData)
        if (this.queryData.method) {
          this.$store.dispatch(this.queryData.method, queryData).then(result => {
            console.log('loadata', result)
            //骨架屏关闭
            this.loading && (this.loading.skeleton = false)
            //调整页码
            this.requery.page = result.page || 1
            this.requery.size = result.size || 20
            this.requery.total = result.total || 0
            this.data_loading = false
            uni.hideLoading()
            return resolve()
          }).catch(_ => {
            console.log('err', _)
            this.data_loading = false
            uni.hideLoading()
            return reject()
          })
        } else {

        }
      })
    }
  }
}
